package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hunliji.monitor.dao.mapper.mt.AlertConfigDAO;
import com.hunliji.monitor.dao.model.dataobject.AlertConfigDO;
import com.hunliji.monitor.dao.model.dataobject.BaseDO;
import com.hunliji.monitor.service.api.AlertConfigService;
import com.hunliji.monitor.service.api.model.bo.AlertConfigBO;
import com.hunliji.monitor.share.component.convertor.ConverterFacade;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * File：AlertConfigServiceImpl.java<br> Title: <br> Description: <br>
 *
 * @author lejin Date: 2022/10/10
 */
@Service
public class AlertConfigServiceImpl implements AlertConfigService {

    @Resource
    AlertConfigDAO alertConfigDAO;

    @Resource
    ConverterFacade converterFacade;

    @Override
    public List<AlertConfigBO> listByQuery(AlertConfigBO alertConfigBO) {
        LambdaQueryWrapper<AlertConfigDO> queryWrapper = new LambdaQueryWrapper<AlertConfigDO>()
            .eq(Objects.nonNull(alertConfigBO.getId()), AlertConfigDO::getId, alertConfigBO.getId())
            .eq(Objects.nonNull(alertConfigBO.getAlertId()), AlertConfigDO::getAlertId, alertConfigBO.getAlertId())
            .eq(Objects.nonNull(alertConfigBO.getProject()), AlertConfigDO::getProject, alertConfigBO.getProject())
            .eq(Objects.nonNull(alertConfigBO.getAlertName()), AlertConfigDO::getAlertName, alertConfigBO.getAlertName())
            .eq(Objects.nonNull(alertConfigBO.getEnableStatus()), AlertConfigDO::getEnableStatus, alertConfigBO.getEnableStatus())
            .like(Objects.nonNull(alertConfigBO.getActionTypes()), AlertConfigDO::getActionTypes, alertConfigBO.getActionTypes())
            .eq(AlertConfigDO::getDeleted, Boolean.FALSE);
        List<AlertConfigDO> alertConfigDOs = alertConfigDAO.selectList(queryWrapper);
        return converterFacade.convertList(alertConfigDOs, AlertConfigBO.class);
    }

    @Override
    public AlertConfigBO getByQuery(AlertConfigBO alertConfigBO) {
        if(Objects.isNull(alertConfigBO.getId()) && Objects.isNull(alertConfigBO.getAlertId()) && Objects.isNull(alertConfigBO.getProject())){
            return null;
        }
        LambdaQueryWrapper<AlertConfigDO> queryWrapper = new LambdaQueryWrapper<AlertConfigDO>()
            .eq(Objects.nonNull(alertConfigBO.getId()), AlertConfigDO::getId, alertConfigBO.getId())
            .eq(Objects.nonNull(alertConfigBO.getAlertId()), AlertConfigDO::getAlertId, alertConfigBO.getAlertId())
            .eq(Objects.nonNull(alertConfigBO.getProject()), AlertConfigDO::getProject, alertConfigBO.getProject())
            .eq(AlertConfigDO::getDeleted, Boolean.FALSE);
        AlertConfigDO alertConfigDO = alertConfigDAO.selectOne(queryWrapper);
        return converterFacade.convert(alertConfigDO, AlertConfigBO.class);
    }

    @Override
    public void insertOne(AlertConfigBO alertConfigBO) {
        if(Objects.isNull(alertConfigBO)){
            return;
        }
        AlertConfigDO alertConfigDO = converterFacade.convert(alertConfigBO, AlertConfigDO.class);
        alertConfigDAO.insert(alertConfigDO);
        alertConfigBO.setId(alertConfigDO.getId());
    }

    @Override
    public void updateOne(AlertConfigBO alertConfigBO) {
        if(Objects.isNull(alertConfigBO)){
            return;
        }
        AlertConfigDO alertConfigDO = converterFacade.convert(alertConfigBO, AlertConfigDO.class);
        alertConfigDAO.updateById(alertConfigDO);
    }

    @Override
    public void deleteOne(Long id) {
        if(Objects.isNull(id)){
            return;
        }
        LambdaUpdateWrapper<AlertConfigDO> updateWrapper = new LambdaUpdateWrapper<AlertConfigDO>()
            .eq(AlertConfigDO::getId, id)
            .set(AlertConfigDO::getDeleted, Boolean.TRUE);
        alertConfigDAO.update(null, updateWrapper);
    }
}
